<?php
class Classifiedscatg_model extends CI_Model{
	/**
	 * @author Pwint Phyu Shwe
	 * Classified Category Model for tbl_category and tbl_adv_category tables
	 * CRUD of Classifieds Category related.
	 * This model include pagination and related functions.
	 * Refer detail in each function's comment section below.
	 * This model is related to classified advertisements.
	 */
	// table name
	private $tbl_category = 'adv_category';
	private $tbl_classified = 'classified';
	
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}
	// get number of users in database
	function count_all(){
		return $this->db->count_all($this->tbl_category);
	}
	
	//get description
	function get_classifieds_categories($cond = NULL){
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		
		$query = $this->db->get($this->tbl_category);
		$result = array();
		$result[''] = 'Select Category';
		foreach ($query->result() as $row){
			$result[$row->id]= $row->cat_descriptions;
		}
		$query->free_result(); //free result
		return $result;
	}
	//get category by id
	function get_category_by_id($cond = NULL){
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		
		$query = $this->db->get($this->tbl_category);
		$result = $query->result();
		$query->free_result(); //free result
		return $result;
	}
	//get categories with total number of advertisement
	function get_cls_type_total($cond = NULL){
		if($cond != NULL){
			foreach ($cond as $param => $item)
				$this->db->where($param, $item);
		}
		$this->db->select('adv_category.id,adv_category.cat_descriptions as name, count(classified.categoryID) as count',FALSE);
		$this->db->from($this->tbl_category);
		
		$this->db->join($this->tbl_classified, 'adv_category.id = classified.categoryID AND expired_date > NOW()', 'left');
		$this->db->group_by("adv_category.id"); 
		
		$query = $this->db->get();
		$results['rows'] = $query->result();
		$query->free_result(); //free result
		return $results;
	}
	//get random category
	function get_random_category() {

		$query = $this->db->query('SELECT * FROM adv_category ORDER BY search_count desc LIMIT 3');
		$results = $query->result();
		$query->free_result();
		return $results;

	}
	//increase search count of the category
	function increase_count($id){
	  $this->db->set('search_count', 'search_count + ' .  1 , FALSE);
      $this->db->where('id', (int)$id);
      $this->db->update($this->tbl_category);
	}
	//delete
	function delete($id){
		$this->db->where('id', $id);
		$this->db->delete($this->tbl_category);
	}
	//save
	function save($obj)
	{
		$this->db->insert($this->tbl_category, $obj);
		return $this->db->insert_id();
	}
	//update
	function  update($classCat) {
		$this->db->where('id', $obj['id']);
		$this->db->update($this->tbl_category, $obj);
		
	}
}
?>